﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO.Admin;
using System.Data;
using BO.Admin;
using BO;

namespace ProjectQuaDep.Admin.News
{
    public partial class NewsList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                showListMenuLv1();
                setSubMenu();
                showAll();
                showListMenuLv1Find();
            }
        }
        protected void showAll()
        {
            string sFind = txtSearch.Text.Trim();
            string sZoneID = "0";
            string checkStatus = rblStatus.SelectedValue.ToString();
            sZoneID = ddlZoneFind.SelectedValue.ToString();
            gvShow.DataSource = NewsBO.getAllNewsBO(sFind, sZoneID, checkStatus);
            gvShow.DataBind();
        }
        protected void showListMenuLv1()
        {
            ddlZone.DataSource = NewsBO.getAllZoneCache();
            ddlZone.DataTextField = BO.Const.tbZoneNews.title;
            ddlZone.DataValueField = BO.Const.tbZoneNews.ZoneId;
            ddlZone.DataBind();
        }
        protected void showListMenuLv1Find()
        {
            DataTable dtListAll = NewsBO.getAllZoneListBO();
            DataTable dtListFilterTo = new DataTable();
            DataTable dtListFilterNho = new DataTable();
            DataTable dtShow = new DataTable();
            dtListFilterTo.Columns.Add(BO.Const.tbZoneNews.ZoneId);
            dtListFilterTo.Columns.Add(BO.Const.tbZoneNews.title);
            dtListFilterTo.Columns.Add(BO.Const.tbZoneNews.ParentId);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneNews.ZoneId);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneNews.title);
            dtListFilterNho.Columns.Add(BO.Const.tbZoneNews.ParentId);
            dtShow.Columns.Add(BO.Const.tbZoneNews.ZoneId);
            dtShow.Columns.Add(BO.Const.tbZoneNews.title);
            dtShow.Columns.Add(BO.Const.tbZoneNews.ParentId);
            DataRow drFirst = dtShow.NewRow();
            drFirst[BO.Const.tbZoneProduct.ZoneId] = "0";
            drFirst[BO.Const.tbZoneProduct.title] = "Tất cả";
            drFirst[BO.Const.tbZoneProduct.ParentId] = "0";
            dtShow.Rows.Add(drFirst);
            //chia ra de add sau
            if (dtListAll.Rows.Count > 0)
            {
                for (int i = 0; i < dtListAll.Rows.Count; i++)
                {
                    if (int.Parse(dtListAll.Rows[i][BO.Const.tbZoneNews.ParentId].ToString()) > 0)
                    {
                        //cat nho
                        DataRow dr = dtListFilterNho.NewRow();
                        dr[BO.Const.tbZoneNews.ZoneId] = dtListAll.Rows[i][BO.Const.tbZoneNews.ZoneId].ToString();
                        dr[BO.Const.tbZoneNews.title] = dtListAll.Rows[i][BO.Const.tbZoneNews.title].ToString();
                        dr[BO.Const.tbZoneNews.ParentId] = dtListAll.Rows[i][BO.Const.tbZoneNews.ParentId].ToString();
                        dtListFilterNho.Rows.Add(dr);
                    }
                    else
                    {
                        //cat to
                        DataRow dr = dtListFilterTo.NewRow();
                        dr[BO.Const.tbZoneNews.ZoneId] = dtListAll.Rows[i][BO.Const.tbZoneNews.ZoneId].ToString();
                        dr[BO.Const.tbZoneNews.title] = dtListAll.Rows[i][BO.Const.tbZoneNews.title].ToString();
                        dr[BO.Const.tbZoneNews.ParentId] = dtListAll.Rows[i][BO.Const.tbZoneNews.ParentId].ToString();
                        dtListFilterTo.Rows.Add(dr);
                    }
                }
            }
            //add vao
            if (dtListFilterTo.Rows.Count > 0)
            {
                for (int i = 0; i < dtListFilterTo.Rows.Count; i++)
                {
                    //add cat to sau do add luon cat con vao
                    DataRow dr = dtShow.NewRow();
                    int ParentId = int.Parse(dtListFilterTo.Rows[i][BO.Const.tbZoneNews.ParentId].ToString());
                    int ID = int.Parse(dtListFilterTo.Rows[i][BO.Const.tbZoneNews.ZoneId].ToString());
                    dr[BO.Const.tbZoneNews.ZoneId] = ID.ToString();
                    dr[BO.Const.tbZoneNews.title] = dtListFilterTo.Rows[i][BO.Const.tbZoneNews.title].ToString();
                    dr[BO.Const.tbZoneNews.ParentId] = ParentId;
                    dtShow.Rows.Add(dr);
                    for (int j = 0; j < dtListFilterNho.Rows.Count; j++)
                    {
                        int ParentId2 = int.Parse(dtListFilterNho.Rows[j][BO.Const.tbZoneNews.ParentId].ToString());
                        if (ParentId2 == ID)
                        {
                            DataRow dr2 = dtShow.NewRow();
                            dr2[BO.Const.tbZoneNews.ZoneId] = dtListFilterNho.Rows[j][BO.Const.tbZoneNews.ZoneId].ToString();
                            dr2[BO.Const.tbZoneNews.title] = "---" + dtListFilterNho.Rows[j][BO.Const.tbZoneNews.title].ToString();
                            dr2[BO.Const.tbZoneNews.ParentId] = dtListFilterNho.Rows[j][BO.Const.tbZoneNews.ParentId].ToString();
                            dtShow.Rows.Add(dr2);
                        }
                    }
                }
            }


            ddlZoneFind.DataSource = dtShow; //NewsBO.getAllZoneCache();
            ddlZoneFind.DataTextField = BO.Const.tbZoneNews.title;
            ddlZoneFind.DataValueField = BO.Const.tbZoneNews.ZoneId;
            ddlZoneFind.DataBind();
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            string ZoneId = ddlZone.SelectedValue.ToString();
            string ZoneIdOther = "";
            string Title = txtTitle.Text;
            string Sapo = txtDesc.Text;
            string Avatar = txtImages.Text;
            string Author = txtTacGia.Text;
            string IsFocus = Utils.getOneSelect(ckFocus);
            string Tags = txtTags.Text;
            string status = Utils.getOneSelect(ckSelect);
            string longContent = txtContent.Text;
            string parentID = "0";
            string nameZoneSmall = "";
            for (int i = 0; i < cblZoneOther.Items.Count; i++)
            {
                //lay ra danh sach thuoc chuyen muc nao
                if (cblZoneOther.Items[i].Selected == true)
                {
                    ZoneIdOther += cblZoneOther.Items[i].Value.ToString() + ",";
                    parentID = cblZoneOther.Items[i].Value.ToString();
                    nameZoneSmall = cblZoneOther.Items[i].Text.ToString();
                }
            }
            string Url = "/" + Utils.BuildLinkUrlNice(nameZoneSmall) + "/" + Utils.BuildLinkUrlNice(Title.Trim()) + "-";
            if (cmdUpdate.Text == "Thêm Mới")
            {
                //them moi
                string kq = NewsBO.addNewsBO(ZoneId, ZoneIdOther, Title, Sapo, Avatar, Author, IsFocus, Url, Tags, status, longContent);
                if (kq == "0")
                {
                    lblMs.Text = "Xảy ra lỗi khi thêm mới bạn hãy xem lại!";
                }
                else
                {
                    NewsBO.UpdateBuildLinkBO();
                    lblMs.Text = "";
                    showAll();
                    pnUpdate.Visible = false;
                }
            }
            else
            {
                //sua chua
                Url = Url + lblID.Text.Trim() + ".htm";
                string kq = NewsBO.EditNewsBO(lblID.Text, ZoneId, ZoneIdOther, Title, Sapo, Avatar, Author, IsFocus, Url, Tags, status, longContent);
                if (kq == "0")
                {
                    lblMs.Text = "Xảy ra lỗi khi cập nhật bạn hãy xem lại!";
                }
                else
                {
                    lblMs.Text = "";
                    showAll();
                    pnUpdate.Visible = false;
                }
            }
        }

        protected void cmdHide_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Trang: " + (gvShow.PageIndex + 1).ToString();
            showAll();
        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdEdit")
            {
                DataTable dt = NewsBO.GetInfomationNewsBO(ID);
                if (dt.Rows.Count > 0)
                {
                    cmdUpdate.Text = "Sửa Chữa";
                    pnUpdate.Visible = true;
                    lblID.Text = ID;
                    txtContent.Text = dt.Rows[0][Const.tbNews.longContent].ToString();
                    txtDesc.Text = dt.Rows[0][Const.tbNews.Sapo].ToString();
                    txtImages.Text = dt.Rows[0][Const.tbNews.Avatar].ToString();
                    txtTacGia.Text = dt.Rows[0][Const.tbNews.Author].ToString();
                    txtTags.Text = dt.Rows[0][Const.tbNews.Tags].ToString();
                    txtTitle.Text = dt.Rows[0][Const.tbNews.Title].ToString();
                    Utils.setOneSelect(ckFocus, dt.Rows[0][Const.tbNews.IsFocus].ToString());
                    Utils.setOneSelect(ckSelect, dt.Rows[0][Const.tbNews.status].ToString());
                    ddlZone.SelectedValue = dt.Rows[0][Const.tbNews.ZoneId].ToString();
                    string OtherCat = dt.Rows[0][Const.tbNews.ZoneIdOther].ToString();
                    setSubMenu();
                    for (int i = 0; i < cblZoneOther.Items.Count; i++)
                    {
                        //lay ra danh sach thuoc chuyen muc nao
                        //if (cblZoneOther.SelectedItem.Selected == true)
                        //{
                        string sValues = cblZoneOther.Items[i].Value.ToString() + ",";//dinh dang la cat, vd 12,
                        if (OtherCat.Contains(sValues))
                        {
                            cblZoneOther.Items[i].Selected = true;
                        }
                        else
                        {
                            cblZoneOther.Items[i].Selected = false;
                        }
                        //}
                    }
                }
            }
            else if (cmdName == "cmdRemove")
            {
                NewsBO.EditStatusNewsBO(ID, "0");
                showAll();
            }
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            showAll();
        }

        protected void cmdAdd_Click(object sender, ImageClickEventArgs e)
        {
            pnUpdate.Visible = true;
            cmdUpdate.Text = "Thêm Mới";
            RsControl();
        }

        protected void ddlZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            setSubMenu();
        }
        protected void RsControl()
        {
            lblID.Text = "";
            txtContent.Text = "";
            txtDesc.Text = "";
            txtImages.Text = "";
            txtTacGia.Text = "";
            txtTags.Text = "";
            txtTitle.Text = "";
            ckFocus.Checked = false;
            ckSelect.Checked = false;

        }
        protected void setSubMenu()
        {
            DataTable dtZoneSub = new DataTable();
            dtZoneSub = NewsBO.getAllZoneByParentIdCache(ddlZone.SelectedValue.ToString());
            cblZoneOther.DataSource = dtZoneSub;
            cblZoneOther.DataValueField = BO.Const.tbZoneNews.ZoneId;
            cblZoneOther.DataTextField = BO.Const.tbZoneNews.title;
            cblZoneOther.DataBind();
        }

        protected void ddlZoneFind_SelectedIndexChanged(object sender, EventArgs e)
        {
            showAll();
        }

        protected void rblStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            showAll();
        }
    }
}